package cn.song.utils;

import lombok.extern.slf4j.Slf4j;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
 * 文件路径
 *
 * @author SongRenShuo
 * @date 2024/07/20
 */
@Slf4j
public class FilePath {


    /**
     * 绝对路径
     */
    public static final String JUEDUI = "C://spring-boot-demosys//";
    /**
     * RabbitMQ日志路径
     */
    public static final String RABBITMQLOG = "C://spring-boot-demosys//RabbitMqLog//";


    /**
     * 相对路径
     */
    public static final String XIANGDUI = "/spring-boot-demofile/";

    /**
     * RabbitMQ日志路径
     */
    public  static  final String RABBITMQLOGXIANGDUI= "/spring-boot-demofile/RabbitMqLog/";

    public static String jueduiLujingByxinagdui(String xiangduilujing) {
        return xiangduilujing.replace("/", "//").replace(FilePath.XIANGDUI.replace("/", "//"), FilePath.JUEDUI);
    }

    /**
     * 图片压缩的方法
     *
     * @param path
     * @param fileName
     */
    public static void yasuo(String path, String fileName) {

        // 获取文件后缀
        File inputImagePath1 = new File(path + fileName);
        String fileName1 = inputImagePath1.getName();
        String fileExtension = "";
        int i = fileName1.lastIndexOf('.');
        if (i > 0) {
            fileExtension = fileName1.substring(i + 1);
        }
        try {
            String inputImagePath = path + fileName; // 输入图片路径
            String outputImagePath = path + fileName; // 输出图片路径
            int targetSizeKB = 50; // 目标压缩大小（KB）
            BufferedImage image = ImageIO.read(new File(inputImagePath));
            Image compressedImage = compressImage(image, targetSizeKB, fileExtension);
            ImageIO.write((BufferedImage) compressedImage, fileExtension, new File(outputImagePath));
        } catch (IOException ex) {
            log.error("压缩图片异常：", ex);
        }
    }

    public static Image compressImage(BufferedImage image, int targetSizeKB, String fileExtension) {
        // 计算目标图片的尺寸
        long targetSizeBytes = targetSizeKB * 1024;
        long originalSizeBytes = getImageSize(image, fileExtension);
        double compressionRatio = (double) targetSizeBytes / originalSizeBytes;
        int targetWidth = (int) (image.getWidth() * Math.sqrt(compressionRatio));
        int targetHeight = (int) (image.getHeight() * Math.sqrt(compressionRatio));
        // 使用ImageIO进行压缩
        BufferedImage compressedImage = new BufferedImage(targetWidth, targetHeight, BufferedImage.TYPE_INT_RGB);
        compressedImage.getGraphics().drawImage(image.getScaledInstance(targetWidth, targetHeight, Image.SCALE_SMOOTH), 0, 0, null);
        return compressedImage;
    }

    public static long getImageSize(BufferedImage image, String fileExtension) {
        File tempFile;
        try {
            tempFile = File.createTempFile("temp", ".tmp");
            ImageIO.write(image, fileExtension, tempFile);
            long size = tempFile.length();
            tempFile.delete();
            return size;
        } catch (IOException ex) {
            ex.printStackTrace();
            return 0;
        }
    }

}
